package logic

import (
	"api/model/order_food_category"
	"api/service"
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderFoodCategoryListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderFoodCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderFoodCategoryListLogic {
	return &OrderFoodCategoryListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderFoodCategoryListLogic) OrderFoodCategoryList() (resp *types.ResponseOrderFoodCategoryList, err error) {
	conn := sqlx.NewMysql(l.svcCtx.Config.DataSource)
	categoryModel := order_food_category.NewOrderFoodCategoryModel(conn)

	busService := service.NewBusinessService(l.ctx, l.svcCtx)
	bus := busService.GetLoginBusinessInfo()

	list, err := categoryModel.List(l.ctx, bus.Id)

	resp = &types.ResponseOrderFoodCategoryList{}
	if list != nil {
		categoryList := make([]types.OrderFoodCategoryList, len(*list))

		for i, item := range *list {
			categoryList[i].Id = item.Id
			categoryList[i].Title = item.Title
			categoryList[i].Sort = item.Sort
		}

		resp.List = categoryList

	}
	resp.Code = 200

	return resp, nil
}
